package com.supeng.PMdepot.activity.bak;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.supeng.PMdepot.R;
import com.supeng.PMdepot.base.BaseActivity;
import com.supeng.PMdepot.common.Config;
import com.supeng.PMdepot.common.Constant;
import com.supeng.PMdepot.entity.ServiceResult;
import com.supeng.PMdepot.event.FinishEvent;
import com.supeng.PMdepot.event.ShuaXinEvent;
import com.supeng.PMdepot.net.Api;
import com.supeng.PMdepot.net.NetUtils;
import com.supeng.PMdepot.util.ToastUtils;
import com.supeng.PMdepot.util.actionbar.LightStatusBarUtils;
import com.supeng.PMdepot.util.actionbar.ToolbarUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 撤销和删除 退回
 * Created by Supeng on 2018/10/12.
 */

public class CheXiaoActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.title_bar)
    RelativeLayout titleBar;
    @BindView(R.id.rgp)
    RadioGroup rgp;
    @BindView(R.id.et_miaoshu)
    EditText etMiaoshu;
    @BindView(R.id.save_btn)
    Button saveBtn;
    @BindView(R.id.root_layout)
    LinearLayout rootLayout;
    @BindView(R.id.rb_chongfu)
    RadioButton rbChongfu;
    @BindView(R.id.rb_wubao)
    RadioButton rbWubao;
    @BindView(R.id.rb_other)
    RadioButton rbOther;
    @BindView(R.id.ll_miaoshu)
    LinearLayout llMiaoshu;
    private String type;
    private String miaoshu;
    private String wxid;
    private String doingType;
    @BindView(R.id.vv_line)
    View vv_line;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getIntent().getStringExtra("type");
        wxid=getIntent().getStringExtra("wxid");
        doingType=getIntent().getStringExtra("doingType");
        //标题栏颜色
        ToolbarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.blueColorAppKey));
        //深色主题 true字颜色为深色 false 为白色标题字
        LightStatusBarUtils.setLightStatusBar(this, false);
        setContentView(R.layout.activity_chexiao);
        unbinder = ButterKnife.bind(this);
        initView();
    }

    private void initView() {
      llMiaoshu.setVisibility(View.GONE);
        vv_line.setVisibility(View.GONE);
        if (type.equals("chexiao")) {
            tvTitle.setText("撤销");
        } else if(type.equals("delete")){
            tvTitle.setText("删除");
        }else if(type.equals("tuihui")){
            tvTitle.setText("退回");
        }
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        rgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_chongfu:
                        llMiaoshu.setVisibility(View.GONE);
                        vv_line.setVisibility(View.GONE);
                        miaoshu = "重复报";
                        break;
                    case R.id.rb_wubao:
                        llMiaoshu.setVisibility(View.GONE);
                        vv_line.setVisibility(View.GONE);
                        miaoshu = "误报";
                        break;
                    case R.id.rb_other:
                        llMiaoshu.setVisibility(View.VISIBLE);
                        vv_line.setVisibility(View.VISIBLE);
                        miaoshu = "";
                        break;
                }
            }
        });
        saveBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.save_btn:
                if (TextUtils.isEmpty(miaoshu)) {
                    if (TextUtils.isEmpty(etMiaoshu.getText().toString())) {
                        ToastUtils.show("原因不能为空");
                        return;
                    } else {
                        miaoshu = etMiaoshu.getText().toString();
                    }
                }

                if (type.equals("chexiao")) {
                    //撤销
                    doShouLi("cx",miaoshu);
                }  else if(type.equals("delete")){
                    //删除
                    doShouLi("sc",miaoshu);
                }else if(type.equals("tuihui")){
                    if(doingType.equals(Constant.YICHULI)&&Config.getUserInfo().getRole().equals(Constant.GUANLIYUAN)) {
                        //激活
                        doShouLi("jh", miaoshu);
                    }else{
                        //退回
                        doShouLi("th", miaoshu);
                    }
                }
                break;
        }
    }
    public void doShouLi(String status,String context){
        Api.updateStatus(getActivity(), wxid, status,context,new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                if(rspData.getRecode()==1){
                    EventBus.getDefault().post(new FinishEvent());
                    EventBus.getDefault().post(new ShuaXinEvent(doingType));
//                    if(type.equals("tuihui")){
//                       /* if(doingType.equals(Constant.ChuLIZHONG)) {
//                            EventBus.getDefault().post(new ShuaXinEvent(Constant.WEICHULI));
//                            EventBus.getDefault().post(new CurrentNowEvent(0));
//                        }else if(doingType.equals(Constant.YICHULI)){
//                            EventBus.getDefault().post(new ShuaXinEvent(Constant.ChuLIZHONG));
//                            EventBus.getDefault().post(new CurrentNowEvent(1));
//                        }*/
//                        EventBus.getDefault().post(new ShuaXinEvent(doingType));
//                    }
//                    if(type.equals("delete")){
////                        EventBus.getDefault().post(new ShuaXinEvent(Constant.CHEXIAO));
////                        EventBus.getDefault().post(new CurrentNowEvent(3));
//                        EventBus.getDefault().post(new ShuaXinEvent(doingType));
//                    }
                    if(type.equals("tuihui")){
                        if(doingType.equals(Constant.ChuLIZHONG)) {
                            EventBus.getDefault().post(new ShuaXinEvent(Constant.WEICHULI));
                           // EventBus.getDefault().post(new CurrentNowEvent(0));
                        }else if(doingType.equals(Constant.YICHULI)){
                            EventBus.getDefault().post(new ShuaXinEvent(Constant.ChuLIZHONG));
                            //EventBus.getDefault().post(new CurrentNowEvent(1));
                        }
                        //EventBus.getDefault().post(new ShuaXinEvent(doingType));
                    }
                    if(type.equals("delete")){
//                        EventBus.getDefault().post(new ShuaXinEvent(Constant.CHEXIAO));
//                        EventBus.getDefault().post(new CurrentNowEvent(3));
                        EventBus.getDefault().post(new ShuaXinEvent(Constant.CHEXIAO));
                    }
                    finish();
                }else{
                    ToastUtils.show(rspData.getMsg());
                }
            }

            @Override
            public void failed(String msg) {
                ToastUtils.show(msg);
            }

            @Override
            public void onUsedCache(String msg) {

            }
        }, ServiceResult.class);
    }

}
